﻿@{
    ViewBag.Title = "返货管理";
    Layout = "~/Views/Shared/_WindowLayout.cshtml";
}
@model TEWorkFlow.Domain.Business.PcReturnManage
@section script
{
    <script type="text/javascript">
        mini.parse();

        var grid = mini.get('datagrid1');
        grid.url = "/Business/GetReturnDetailList/" + entityId;
        grid.reload();

        function onActionRenderer(e) {
            var grid = e.sender;
            var record = e.record;
            var uid = record._uid;
            var rowIndex = e.rowIndex;

            var s = '<a class="New_Button" href="javascript:newRow()">增加</a>&nbsp;'
                    + ' <a class="Edit_Button" href="javascript:editRow(\'' + uid + '\')" >编辑</a>&nbsp;'
                    + ' <a class="Delete_Button" href="javascript:delRow(\'' + uid + '\')">删除</a>';

            if (grid.isEditingRow(record)) {
                s = '<a class="Update_Button" href="javascript:updateRow(\'' + uid + '\')">保存</a>&nbsp;'
                    + '<a class="Cancel_Button" href="javascript:cancelRow(\'' + uid + '\')">取消</a>'
            }
            return s;
        }
        function newRow() {
            if (grid.isEditing()) {
                if (grid.isEditingRow(grid.getRow(0)) && grid.getRow(0)._state == "added") {
                    return;
                }
                grid.cancelEdit();
            }
            var row = {};
            grid.addRow(row, 0);

            grid.cancelEdit();
            grid.beginEditRow(row);
        }
        function editRow(row_uid) {
            if (grid.isEditing()) {
                if (grid.isEditingRow(grid.getRow(0)) && grid.getRow(0)._state == "added") {
                    return;
                }
                grid.cancelEdit();
            }
            var row = grid.getRowByUID(row_uid);
            if (row) {
                grid.cancelEdit();
                grid.beginEditRow(row);
            }
        }
        function cancelRow(row_uid) {
            grid.reload();
        }
        function delRow(row_uid) {
            var row = grid.getRowByUID(row_uid);
            if (row) {
                if (confirm("确定删除此记录？")) {
                    grid.loading("删除中，请稍后......");
                    $.ajax({
                        url: "/Business/DeleteReturnDetail/" + row.Id,
                        success: function (text) {
                            grid.reload();
                        },
                        error: function () {
                        }
                    });
                }
            }
        }

        function updateRow(row_uid) {
            var row = grid.getRowByUID(row_uid);
            if (grid.isValid() == false) {
                return false;
            }
            grid.commitEdit();
            var rowData = grid.getChanges()[0];
            if (!rowData) {
                //没有改动
                return;
            }
            if (!rowData.GoodsCode) {
                alert("没有选择产品");
                grid.reload();
                return;
            }

            grid.loading("保存中，请稍后......");
            var json = mini.encode(rowData);

            $.ajax({
                url: "/Business/SaveGoodsToReturn/" + entityId,
                data: { "data": json },
                type: "post",
                success: function (text) {
                    grid.reload();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                }
            });

        }
        function onDCodeButtonEdit(e) {
            var btnEdit = this;
            mini.open({
                url: "/Account/DepartmentSelectTree",
                showMaxButton: false,
                title: "选择部门",
                width: 350,
                height: 350,
                ondestroy: function (action) {
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.GetData();
                        data = mini.clone(data);
                        if (data) {
                            btnEdit.setValue(data.id);
                            btnEdit.setText(data.text);
                        }
                    }
                }
            });
        }
        function onSupCodeValueChanged(e) {
            var item = e.selected;
            if (item) {
                mini.get("EnCode").setValue(item.Id);
            }
        }
        function onGoodsCodeButtonEdit(e) {
            var btnEdit = this;
            mini.open({
                url: "/Archives/GoodsSelectWindow",
                showMaxButton: false,
                title: "选择商品",
                width: 400,
                height: 350,
                ondestroy: function (action) {
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.GetData();
                        data = mini.clone(data);
                        if (data) {
                            btnEdit.setValue(data.Id);
                            btnEdit.setText(data.GoodsName);
                        }
                    }
                }
            });
        }
        var issaving = false;
        function SaveData() {
            var form = new mini.Form("form1");
            var o = form.getData(true);

            form.validate();
            if (form.isValid() == false) return;

            var json = mini.encode([o]);
            issaving = true;
            $.ajax({
                type: "post",
                url: "/Business/SaveReturn",
                data: o,
                cache: false,
                success: function (text) {
                    issaving = false;
                    CloseWindow("save");
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    issaving = false;
                    alert(jqXHR.responseText);
                    CloseWindow();
                }
            });
        }
        document.onkeydown = function (e) {
            e = e || window.event;
            if (e.keyCode == 13) {
                //return
                if (grid.isEditing()) {
                    //grid.commitEdit();
                    if (grid.getChanges().length > 0 && grid.getChanges()[0].GoodsCode != undefined) {
                        updateRow();
                    }
                }
                else {
                    newRow();
                }
                return false;
            }

            //ctrl+s
            if (e.keyCode == 83 && e.ctrlKey) {
                if (issaving == false) {
                    SaveData();
                }
                return false;
            }
            //ecs
            if (e.keyCode == 27) {
                grid.reload();
                return false;
            }
        };
    </script>
}
<table class="edittable" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td style="width: 60px;">
            返货单号:
        </td>
        <td style="width: 140px;">
            <input name="Id" value="@Model.Id" class="mini-textbox" required="true"  enabled="false"/>
        </td>
        <td style="width: 60px;">
            采购单号:
        </td>
        <td style="width: 140px;">
            <input name="PcNumber" value="@Model.PcNumber" text="@Model.PcNumber" class="mini-autocomplete" require="true" valueField="Id" displayField="Id" url="/Business/SearchAllPurchaseList" required="true"/>
        </td>
        <td style="width: 60px;">
            供货商:
        </td>
        <td style="width: 140px;">
            <input name="SupCode" value="@Model.SupCode" text="@Model.SupCode" class="mini-autocomplete" require="true" valueField="SupName" displayField="SupName" url="/Archives/GetAllSupplierArchive" onvaluechanged="onSupCodeValueChanged"   enabled="false"/>
            <input id="EnCode" name="EnCode" value="@Model.EnCode" readonly="readonly" class="mini-hidden" required="true"/>
        </td>
    </tr>
    <tr>
        <td style="width: 60px;">
            采购形式:
        </td>
        <td style="width: 140px;">
            <input name="PcForm" value="@Model.PcForm" class="mini-textbox"/>
        </td>
        <td style="width: 60px;">
            门店:
        </td>
        <td style="width: 140px;">
            <input name="bCode" class="mini-combobox"  textField="bName" valueField="Id" emptyText="请选择..." url="/Archives/SearchAllranches" value="@Model.bCode"  allowInput="false" showNullItem="true" nullItemText="请选择..." required="true"/>
        </td>
        <td style="width: 60px;">
            返货日期:
        </td>
        <td style="width: 140px;">
            <input name="ReturnDate" value="@Model.ReturnDate" class="mini-datepicker"/>
        </td>
    </tr>
    <tr>
        <td style="width: 60px;">
            创建时间:
        </td>
        <td style="width: 140px;">
            <input name="CreateDate" value="@Model.CreateDate" class="mini-datepicker"/>
        </td>
        <td style="width: 60px;">
            操作员:
        </td>
        <td style="width: 140px;">
            <input name="Operator" class="mini-combobox"  textField="text" valueField="id" emptyText="请选择..." url="/Category/AllUsers" value="@Model.Operator"  allowInput="false" showNullItem="true" nullItemText="请选择..."/>
        </td>
        <td style="width: 60px;">
            @*审核人:*@
        </td>
        <td style="width: 140px;">
            @*<input name="Assessor" class="mini-combobox"  textField="text" valueField="id" emptyText="请选择..." url="/Category/AllUsers" value="@Model.Assessor"  allowInput="false" showNullItem="true" nullItemText="请选择..."/>*@
        </td>
    </tr>
    <tr>
        <td style="width: 60px;">
            是否审核:
        </td>
        <td align="left" style="padding-left: 2px;">
            <div name="IfExamine" class="mini-checkbox" checked="@Model.IfExamine" readOnly="false" text="审核"  trueValue="1" falseValue="0">
            </div>
        </td>
        <td style="width: 60px;">
            开始时间:
        </td>
        <td style="width: 140px;">
            <input name="ExamineDate" value="@Model.ExamineDate" class="mini-datepicker"/>
        </td>
        <td style="width: 60px;">
            结束时间:
        </td>
        <td style="width: 140px;">
            <input name="OperatorDate" value="@Model.OperatorDate" class="mini-datepicker"/>
        </td>
    </tr>
</table>
<div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 250px;" idfield="Id"
    emptytext="数据为空，<a href='javascript:newRow()'>增加一条</a>" showemptytext="true"
    showpager="false">
    <div property="columns">
        <div field="GoodsCode" headeralign="center" allowsort="true" displayfield="GoodsName">
            产品 @*<input property="editor" class="mini-buttonedit" onbuttonclick="onGoodsCodeButtonEdit"
                style="width: 100%;" />*@
            <div property="editor" class="mini-autocomplete" textfield="GoodsName" popupwidth="400"
                valuefield="Id" url="/Archives/GetAllGoodsArchive">
                <div property="columns">
                    <div header="店内码" field="Id" width="30">
                    </div>
                    <div header="名称" field="GoodsName">
                    </div>
                    <div header="条形码" field="GoodsBarCode">
                    </div>
                </div>
            </div>
        </div>
        <div field="ReturnQty" width="60" headeralign="center" align="center" allowsort="true">
            数量
            <input property="editor" class="mini-spinner" style="width: 100%;" minvalue="1" maxvalue="5000" />
        </div>
        <div field="PurchasePrice" width="60" headeralign="center" align="center" allowsort="true"
            datatype="currency" currencyunit="￥">
            单价
        </div>
        <div field="ReturnMoney" width="60" headeralign="center" align="center" allowsort="true"
            datatype="currency" currencyunit="￥">
            金额
        </div>
        <div field="GoodsBarCode" width="80" headeralign="center" align="center" allowsort="true">
            条码
        </div>
        <div name="action" width="100" headeralign="center" align="center" renderer="onActionRenderer"
            cellstyle="padding:0;">
            操作</div>
    </div>
</div>
